package com.noble.library.utils;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyCharacterMap;
import android.view.ViewConfiguration;

/**
 * 屏幕相关工具类
 */
public class ScreenUtils {
    private ScreenUtils() {
        throw new AssertionError();
    }

    /**
     * 屏幕宽度
     */
    private static int screenWidth = 0;

    public static int getScreenWidth(Context context) {
        if (screenWidth <= 0) {
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            screenWidth = dm.widthPixels;
        }
        return screenWidth;
    }

    /**
     * 屏幕高度
     */
    private static int screenHeight = 0;

    public static int getScreenHeight(Context context) {
        if (screenHeight <= 0) {
            DisplayMetrics dm = context.getResources().getDisplayMetrics();
            screenHeight = dm.heightPixels;
        }
        return screenHeight;
    }

    /**
     * 获取状态栏高度
     */
    private static int statusBarHeight = 38;

    public static int getStatusBarHeight(Context context) {
        if (statusBarHeight == 38) {
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
            }
        }
        return statusBarHeight;
    }

    /**
     * 获取SystemBar高度
     */
    public static int getSystemBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("system_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 获取NavigationBar高度
     */
    public static int getNavigationBarHeight(Activity activity) {
        if (!isNavigationBarShow(activity)) {
            return 0;
        } else {
            int height = 0;
            Resources resources = activity.getResources();
            int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                height = resources.getDimensionPixelSize(resourceId);
            }
            return height;
        }
    }

    private static boolean isNavigationBarShow(Activity activity) {
        if (Build.VERSION.SDK_INT >= 17) {
            Display display = activity.getWindowManager().getDefaultDisplay();
            Point size = new Point();
            Point realSize = new Point();
            display.getSize(size);
            display.getRealSize(realSize);
            return realSize.y != size.y;
        } else {
            boolean menu = ViewConfiguration.get(activity).hasPermanentMenuKey();
            boolean back = KeyCharacterMap.deviceHasKey(4);
            return !menu && !back;
        }
    }

    /**
     * 获取屏幕密度density
     */
    public static float getDensity(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }

    /**
     * 获取屏幕转换的倍数，主要是XML中的dp单位对px的转换
     */
    public static float toDpMultiple(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics.density;
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return Math.round(dpValue * scale + 0.5f);
    }

    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return Math.round(pxValue / scale + 0.5f);
    }

    public static int dp2px(Context context, int dp) {
        return Math.round((float) dp * context.getResources().getDisplayMetrics().density);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变。
     */
    public static int px2sp(Context context, float pxValue) {
        final float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
        return Math.round(pxValue / scaledDensity);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变。
     */
    public static int sp2px(Context context, float spValue) {
        final float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
        return Math.round(spValue * scaledDensity);
    }

    /**
     * 是否是锁屏状态
     */
    public static boolean isScreenLock(Context context) {
        try {
            KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
            return mKeyguardManager.inKeyguardRestrictedInputMode();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
